package myself.doit.schedule.v1;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * Created by bjliuyong on 2017/1/10.
 */
public class SchedulerMain02 {

    public static void main(String args[]) throws Exception {

        SchedulerMain02 schedulerMain = new SchedulerMain02() ;
        Method method = schedulerMain.getClass().getMethod("echo");

        ScheduledMethodRunnable
                scheduledMethodRunnable = new ScheduledMethodRunnable(schedulerMain , method) ;


        CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator("0/1 * * * * ?");

        Date nextTime = new Date() ;
        Date actTime  = nextTime  ;
        while (true) {
            nextTime = cronSequenceGenerator.next(actTime) ;
            long delayTime = nextTime.getTime() - actTime.getTime() ;
            Thread.sleep(delayTime);
            actTime = new Date();
            scheduledMethodRunnable.run();
        }

    }


    public void echo() {
        System.out.println("this is SchedulerMain02 " + CronSequenceGenerator.format(new Date()));
    }


}
